package com.project.controller;

import com.project.bean.UserBean;
import com.project.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;

@RestController
@RequestMapping("user")
public class UserController {
    @Autowired
    private IUserService service;

    /**
     * 登陆
     * @param userName 用户名
     * @param pwd 密码
     * @param request 请求对象
     * @return 登陆成功返回“ok”，否则返回"no"
     */
    @RequestMapping("login")
    public String login(String userName, String pwd,
                        HttpServletRequest request){
        //调用业务方法，完成登陆查询
        UserBean user = service.login(userName,pwd);
        if(user != null){
            //在会话范围内，绑定登陆用户
            request.getSession().setAttribute("loginUser",user);
            return "ok";
        }

        return "no";
    }

    /**
     * 得到登陆用户
     * @return 登陆用户对象
     */
    @RequestMapping("getLoginUser")
    public UserBean getLoginUser(HttpServletRequest request){
        UserBean user = (UserBean)request.getSession()
                .getAttribute("loginUser");

        return user;
    }
}
